<?php
namespace Admin\Controller;

use Admin\Controller\Tencentyun\Conf;
use Admin\Controller\Tencentyun\ImageV2;
use Common\Controller\AdminbaseController;
use Common\Model\NewsColumnTypeModel;
use Common\Model\NewsModel;
use Common\Model\NewsRecommendModel;
use Common\Model\NewsSourceModel;
use Think\Cache\Driver\Redis;
use Common\Model\NewsRewardModel;

class NewsManageController extends AdminbaseController{

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 快捷发布
     */
    public function publish(){
    	$news_id = I("news_id");
        $column_id = I("column_id") ? I("column_id") : '';
        $area_id   = I("area_id") ? I("area_id") : '';
        $add_from  = I("add_from") ? I("add_from") : 0;
        $live_id   = I("live_id") ? I("live_id") : 0;
        //返回 老版本 1，前端交互0 ，默认0
        $return_val = I("return_val") ? I("return_val") : 0;
        $id_type = '';
        $id_val  = '';
        if($column_id){
            $id_type = "column_id";
            $id_val  = $column_id;
        }else{
            $id_type = "area_id";
            $id_val  = $area_id;
        }
        $type = I("type",'1'); //默认为1,新闻列表，2时为专题下的新闻列表
        $type_id = I("type_id",''); //对应的专题分栏的id
        $special_id = I("special_id"); //专题id   专题 添加快捷发布传值

        if(!$id_type){
            $this->error("查找新闻失败！");
        }
        
        if($type == '2'){
            //查看所有分栏
        	$special_column_type = M("vrnews1.column_type",'special_');
        	$special_column_list = $special_column_type->where('status = 1 and news_id = '.$special_id)->field('type_id,name')->select();
        	$this->assign("type", $type);
        	$this->assign("type_id", $type_id);
        	$this->assign("special_id", $special_id);
        	$this->assign("special_column_type_list", $special_column_list);
        }else{
            $column_type_list = array();
            if($column_id) {
                $column_type = M("vrnews1.column_type", 'news_');
                $column_type_list = $column_type->where(array("column_id" => $column_id, 'status' => 1))->field("type_id, name")->select();
            }
            $this->assign("type_list", $column_type_list);
        }
        $news = M("vrnews1.news", NULL);
        $news_info = $news->join("vrnews1.news_video on news_video.news_id = news.news_id",'left')
            ->where(array('news.news_id'=>$news_id))->find();
        $relation = new NewsRecommendModel();
        $news_info['relation'] = $relation->getNewsRelation($news_id);
        if($news_id) $news_info['news_id'] = $news_id;
        $this->assign("news_info", $news_info);
        $this->assign("type_id", $type_id);
        $this->assign("special_id", $special_id);
        $this->assign("news_id", $news_id);
        $this->assign("id_type", $id_type);
        $this->assign("id_val", $id_val);
        $this->assign("live_id",$live_id);
        $this->assign("add_from",$add_from);
        $this->assign("return_val",$return_val);

        $this->display();
    }

    /**
     * 新闻发布入库
     */

    public function add_news(){
    	$news_id   = I("post.news_id","");

        $add_from  = I("get.add_from",0);
        $column_id = I("get.column_id") ? I("get.column_id") : '';
        $area_id   = I("get.area_id") ? I("get.area_id") : '';

        if($column_id){
            $id_type = "column_id";
            $id_val  = $column_id;
        }else{
            $id_type = "area_id";
            $id_val  = $area_id;
        }

        $role_live_str = I("post.role_live_id",''); //管理员填写的 直播ID信息
        $from_status = 0; //直播ID 来源于创建
        if(strpos($role_live_str,'&')) {
            //创建
            $role_live_id = substr($role_live_str, 3, strpos($role_live_str, '&') - 3); //直播ID
        }else{
            //编辑
            $role_live_id = $role_live_str;
            $from_status = 1; //直播ID 来源于编辑
        }


        $data['title']    = I("post.title");
        $data['subtitle'] = I("post.subTitle");
        $data['keywords'] = I("post.keyword");
        $source           = I("post.source");
        $data['weight']   = (int)(I("post.weight") ? I("post.weight") :'70');
        $data['tags']     = I("post.tags");
        $data['abstract'] = I("post.abstract");
        $data['top_status']  = I("post.top_status");
        $data['special_news_id']  = !empty(I("post.special_id")) ? I("post.special_id") : '0';
        $data['type_id']     = !empty(I("post.column_type")) ? I("post.column_type") : (I("post.special_type") ? I("post.special_type") : 0);
        $water               = I("post.water");
        $pub                 = I("post.pub");
        $data['cover_image'] = I("post.cover_image",'');
        $reference           = I("post.reference");
        $data['outer_url']   = I("post.outer_url");
        $data['external_link']   = I("post.external_link");
        $outer_url_pub    = I("post.outer_url_pub");
        $data['vote_id']    = I("post.vote_id");

        if($outer_url_pub == '1'){
        	if(empty($data['outer_url'])){
        		return $this->error("无外链地址！");
        	}
        	$data['outer_url_ishot'] = '1';
        }else{
        	$data['outer_url_ishot'] = '0';
        }
        $news_recommend      = I("post.news_recommend");
        $data['is_watermark']      = I("post.water");

        $data['content'] = json_encode($this->setWatermark($news_id, $data['is_watermark'], $_POST['newsContent']));
        
        
        if(!$news_id){
            $data['news_id'] = time().$this->getRange();
//            $news_id = $data['news_id'];
        }
        $data['source_name'] = $source;
        $data['column_id']   = I("post.column_id") ? I("post.column_id") : '';
        $data['area_id']     = I("post.area_id") ? I("post.area_id") : '';
        $id_type = '';
        $id_val  = '';
        if(I("post.column_id")){
            $id_type = "column_id";
            $id_val  = I("post.column_id");
        }else{
            $id_type = "area_id";
            $id_val  = I("post.area_id");
        }

        if(mb_strlen(trim($data['subtitle']),'UTF8') > 10){
        	return $this->error("副标题不能超过10个字");
        }
        
        if (!preg_match('/^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/', $data['weight'])) {
            return $this->error("权重必须为0到255的数字！");
        }
        
        if($_FILES['imageFile']['name']){
        	$newsModel = new NewsModel();  //上传封面图
        	$imageUrl = $newsModel->uploadImage();
        	$data['cover_image'] = $imageUrl;

            $data['type'] = I("post.type") ==2 ? 2 : 7;


        }else{
        	$data['type']  = I("post.type") ? I("post.type") : 6;
        }

        //保存直播ID，保存直播中新闻 ID  同时 更改为直播对应的状态
        if(!empty($role_live_id)){
            //查看 直播类型
            $live_info = M("vrlive.live")->where(array('live_id' => $role_live_id))->find();
            if($live_info['category'] == 1){
                $data['type'] = 9;
            }else if($live_info['category'] == 2){
                $data['type'] = 10;
            }else if($live_info['category'] == 3){
                $data['type'] = 11;
            }else if($live_info['category'] == 4){
                $data['type'] = 12;
            }else if($live_info['category'] == 5){
                $data['type'] = 13;
            }
            if($from_status == 0) { //创建新闻 关联 直播ID
//                if(!empty($live_info['news_id'])){
//                    return $this->error("此直播已被绑定，请使用引用功能！");
//                }else{ //在直播内 添加 新闻ID ，新闻内添加 直播ID
                    $live_data['news_id'] = $news_id ? $news_id : $data['news_id'];
                    M("vrlive.live")->where('live_id = '.$role_live_id)->data($live_data)->save();
                    $data['live_id']     = $role_live_id;
                    $data['live_status'] = 1;
//                }
            }
        }

        
        if(!empty($source)){
        	$sourceObj = M("vrnews1.news_source", NULL);
        	$source_item = $sourceObj->where(array('name' => $source))->find();
        	if(empty($source_item)){
        		$sourcelist['name'] = $source;
        		$sourcelist['create_time'] = date('Y-m-d H:i:s', time());
        		$sourcelist['creator_id']  = $_SESSION['ADMIN_ID'];
        		$sourceObj->add($sourcelist);
        		$data['source_id'] = $sourceObj->getLastInsID();
        	}else{
        		$data['source_id'] = $source_item['source_id'];
        	}
        }


        if(!empty($pub)){
            if(in_array(1, $pub)){
    			$data['app_pub'] = 1;
    		}else{
    			$data['app_pub'] = 0;
    		}
    		if(in_array(2, $pub)){
    			$data['web_pub'] = 1;
    		}else{
    			$data['web_pub'] = 0;
    		}
        }

        if(!empty($reference)){
            $id   = substr($reference, 3);
            $type = substr($reference, strrpos($reference, '=')+1);
            $data['reference_id']   = substr($id,0, strpos($id, '&'));
            $data['reference_type'] = $type;

        }
        
        if(IS_POST){
        	//解析内部url 如果有id 匹配原有数据
        	$diff = parse_str($_POST['reference']);
        	if($id && $type != 5 && $type != 2){
        		$info = M("vrnews1.news", NULL)->where(array('news_id'=>$id))->field('type, content')->find();
        		if($info['type'] == 5){
        			//如果是图集  取出对应的内容
        			$data['content'] = $info['content'];
        			$data['type']    = $info['type'];
        		}else{
        			if(!empty($data['cover_image'])){
        				$data['type'] = 7;
        			}else{
        				$data['type'] = 6;
        			}
        		}
        	}
        }
        
        $data['re_time'] = time();
        $news = M("vrnews1.news", NULL);
		
       /*  if(IS_POST){
        	//解析内部url 如果有id 匹配原有数据
        	$diff = parse_str($_POST['reference']);
        	if($id && $type != 5 && $type != 2){
        		$info = M("vrnews1.news", NULL)->where(array('news_id'=>$id))->field('type, content')->find();
        		if($info['type'] == 5){
        			//如果是图集  取出对应的内容
        			$data['content'] = $info['content'];
        			$data['type']    = $info['type'];
        		}else{
        			$data['type']    = $info['type'];
        		}
        	}
        } */
        ob_start();
        $jssdk = new JssdkController("wx46fc101ae0317865", "ade9338f307169231203492f5e248ec6");
        if(!$news_id){
            $data['create_time'] = date('Y-m-d H:i:s', time());
        }else{
            $news_info = $news->where('news_id = '.$news_id)->field('create_time')->find();
            $data['create_time'] = $news_info['create_time'];
            $data['update_time'] = time();
        }

        $data['refresh_time'] = time();
        if($news_id){
            $share = C("admin_host").'/text/' . $news_id . '.html';
            $file_name = SITE_PATH.'/data/html/text/'.$news_id.'.html';
        }else{
            $share = C("admin_host").'/text/' . $data['news_id'] . '.html';
            $file_name = SITE_PATH.'/data/html/text/'.$data['news_id'].'.html';
        }
        if($type != 2){
            require_once (SITE_PATH.'/template/image_text_batch.html');
            $content = ob_get_contents();
            if(!$content){
                return $this->error("生产静态页面失败");
            }
            $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http';
            $jssdk = new JssdkController("wx46fc101ae0317865", "ade9338f307169231203492f5e248ec6");
            $signPackage = $jssdk->GetSignPackage();
            $appId = $signPackage["appId"];
            $timestamp = $signPackage["timestamp"];
            $nonceStr = $signPackage["nonceStr"];
            $signature = $signPackage["signature"];
//            $share = C("admin_host").'/text/' . $data['news_id'] . '.html';
            if($data['new_abstract']){
                $description = $data['new_abstract'];
            }else{
                if($data['abstract']){
                    $description =  $data['abstract'];
                }else{
                    $description =  'live.lawnewsw.com';
                }
            }
            if($data['new_cover_image']){
                $cover_img =  $data['new_cover_image'];
            }else{
                if($data['cover_image']){
                    $cover_img = $data['cover_image'];
                }else{
                    $cover_img = C('h5_host').'/public/H5/images/default.png';
                }
            }
            if($data['new_cover_image']){
                $news_cover_img =  $data['new_cover_image'];
            }else{
                if($data['cover_image']){
                    $news_cover_img =  $data['cover_image'];
                }else{
                    $news_cover_img =  C('h5_host').'/public/H5/images/default.png';
                }
            }
            if($data['new_abstract']){
                $new_abstract = $data['new_abstract'];
            }else{
                if($data['abstract']){
                    $new_abstract = $data['abstract'];
                }else{
                    $new_abstract = 'live.lawnewsw.com';
                }
            }
            $time = date('m月d日 H:i',strtotime($data['create_time']));
            if($data['source_name']) {
                $source = '来源：<span ><a style="color:#129aee; display:inline;" href="#" onclick="toSourceId(\'' . $data["source_id"] . '\',\'' . $data["source_name"] . '\')" >' . $data["source_name"] . '</a></span>';
            }else{
                $source = '';
            }
            if($data['type'] == 7){
                $news_content = $this->handleImage($data['content']);
            }else{
                $news_content = json_decode($data['content']);
            }
            if(!empty($data['outer_url'])) {
                $out_url = '<div class="ReadText" style="float:right;margin:0 5%;font-size:1.3rem"><a href="' . $data['outer_url'] . '" style="color: #2ea7e0;">阅读原文</a></div>';
            }else{
                $out_url = '';
            }
            if($data['new_title']){
                $title = $data['new_title'];
            }else{
                $title = $data['title'];
            }
            $h5_host = C("h5_host");
            $host    = C("host");
            $content = str_replace('{{appId}}',$appId ,$content );
            $content = str_replace('{{timestamp}}',$timestamp ,$content );
            $content = str_replace('{{nonceStr}}',$nonceStr ,$content );
            $content = str_replace('{{signature}}',$signature ,$content );
            $content = str_replace('{{share}}',$share ,$content );
            $content = str_replace('{{description}}',$description ,$content );
            $content = str_replace('{{title1}}',$title ,$content );
            $content = str_replace('{{cove_img}}',$cover_img ,$content );
            $content = str_replace('{{new_cover_image}}',$news_cover_img ,$content );
            $content = str_replace('{{title}}',$data['title'] ,$content );
            $content = str_replace('{{abstract}}',$new_abstract ,$content );
            $content = str_replace('{{data}}',$time ,$content );
            $content = str_replace('{{source}}',$source ,$content );
            $content = str_replace('{{content}}',$news_content ,$content );
            $content = str_replace('{{out_url}}',$out_url ,$content );
            $content = str_replace('{{h5_host}}',$h5_host ,$content );
            $content = str_replace('{{host}}',$host ,$content );
            file_put_contents($file_name, $content);
            ob_end_clean();
        }
        if($news_id){
            //更新静态文件
        	$news->where('news_id = '.$news_id)->data($data)->save();
        }else{
        	/* 复制新闻 */
        	if($type == 2){
        		$_news_data = $news->where(array('news_id'=>$id))->find();
        		if(!$_news_data){$this->error('没有找到复制的新闻，请检查ID');exit();}
        		/* 轮播图只能复制图文的新闻 */
        		if(I('type') == 2 && $_news_data['type'] != 7){
        			$this->error('轮播图只能复制图文消息');
        			exit();
        		}
        		if(in_array($_news_data['type'], '2, 5, 6, 7')){
        			$this->error('只能复制文本，图文，视频，图集等新闻');
        			exit();
        		}
        		$data['title']    		  = $_news_data['title'];
        		$data['subtitle'] 		  = $_news_data['subtitle'];
        		$data['keywords']		  = $_news_data['keywords'];
        		$data['source_id']		  = $_news_data['source_id'];
        		$data['source_name'] 	  = $_news_data['source_name'];
        		$data['tags'] 			  = $_news_data['tags'];
        		$data['abstract'] 		  = $_news_data['abstract'];
        		$data['cover_image'] 	  = $_news_data['cover_image'];
        		$data['content'] 		  = $_news_data['content'];
        		$data['outer_url'] 		  = $_news_data['outer_url'];
        		$data['outer_url_ishot']  = $_news_data['outer_url_ishot'];
        		$data['app_pub'] 		  = $_news_data['app_pub'];
        		$data['web_pub'] 	 	  = $_news_data['web_pub'];
        		//$data['is_watermark'] = $_news_data['is_watermark'];
        		$data['weight'] 		  = $_news_data['weight'];
        		$data['top_status'] 	  = $_news_data['top_status'];
        		$data['full_status'] 	  = $_news_data['full_status'];
        		$data['full_title'] 	  = $_news_data['full_title'];
        		$data['full_subtitle']	  = $_news_data['full_subtitle'];
        		$data['full_cover_image'] = $_news_data['full_cover_image'];
        		$data['app_pub'] 		  = $_news_data['app_pub'];
        		$data['type']		      = $_news_data['type'];
        		$data['reference_id']	  = 0;
        		$data['reference_type']   = 0;
                ob_start();
                
                require_once (SITE_PATH.'/template/image_text_batch.html');
                $content = ob_get_contents();
                if(!$content){
                    return $this->error("生产静态页面失败");
                }
                $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http';
                $jssdk = new JssdkController("wx46fc101ae0317865", "ade9338f307169231203492f5e248ec6");
                $signPackage = $jssdk->GetSignPackage();
                $appId = $signPackage["appId"];
                $timestamp = $signPackage["timestamp"];
                $nonceStr = $signPackage["nonceStr"];
                $signature = $signPackage["signature"];
                if($data['new_abstract']){
                    $description = $data['new_abstract'];
                }else{
                    if($data['abstract']){
                        $description =  $data['abstract'];
                    }else{
                        $description =  'live.lawnewsw.com';
                    }
                }
                if($data['new_cover_image']){
                    $cover_img =  $data['new_cover_image'];
                }else{
                    if($data['cover_image']){
                        $cover_img = $data['cover_image'];
                    }else{
                        $cover_img = C('h5_host').'/public/H5/images/default.png';
                    }
                }
                if($data['new_cover_image']){
                    $news_cover_img =  $data['new_cover_image'];
                }else{
                    if($data['cover_image']){
                        $news_cover_img =  $data['cover_image'];
                    }else{
                        $news_cover_img =  C('h5_host').'/public/H5/images/default.png';
                    }
                }
                if($data['new_abstract']){
                    $new_abstract = $data['new_abstract'];
                }else{
                    if($data['abstract']){
                        $new_abstract = $data['abstract'];
                    }else{
                        $new_abstract = 'live.lawnewsw.com';
                    }
                }
                $time = date('m月d日 H:i',strtotime($data['create_time']));
                if($data['source_name']) {
                    $source = '来源：<span ><a style="color:#129aee; display:inline;" href="#" onclick="toSourceId(\'' . $data["source_id"] . '\',\'' . $data["source_name"] . '\')" >' . $data["source_name"] . '</a></span>';
                }else{
                    $source = '';
                }
                if($data['type'] == 7){
                    $news_content = $this->handleImage($_news_data['content']);
                }else{
                    $news_content = json_decode($_news_data['content']);
                }
                if(!empty($data['outer_url'])) {
                    $out_url = '<div class="ReadText" style="float:right;margin:0 5%;font-size:1.3rem"><a href="' . $data['outer_url'] . '" style="color: #2ea7e0;">阅读原文</a></div>';
                }else{
                    $out_url = '';
                }
                if($data['new_title']){
                    $title = $data['new_title'];
                }else{
                    $title = $data['title'];
                }
                $h5_host = C("h5_host");
                $host    = C("host");
                $content = str_replace('{{appId}}',$appId ,$content );
                $content = str_replace('{{timestamp}}',$timestamp ,$content );
                $content = str_replace('{{nonceStr}}',$nonceStr ,$content );
                $content = str_replace('{{signature}}',$signature ,$content );
                $content = str_replace('{{share}}',$share ,$content );
                $content = str_replace('{{description}}',$description ,$content );
                $content = str_replace('{{title1}}',$title ,$content );
                $content = str_replace('{{cove_img}}',$cover_img ,$content );
                $content = str_replace('{{new_cover_image}}',$news_cover_img ,$content );
                $content = str_replace('{{title}}',$data['title'] ,$content );
                $content = str_replace('{{abstract}}',$new_abstract ,$content );
                $content = str_replace('{{data}}',$time ,$content );
                $content = str_replace('{{source}}',$source ,$content );
                $content = str_replace('{{content}}',$news_content ,$content );
                $content = str_replace('{{out_url}}',$out_url ,$content );
                $content = str_replace('{{h5_host}}',$h5_host ,$content );
                $content = str_replace('{{host}}',$host ,$content );
                file_put_contents($file_name, $content);
                ob_end_clean();
        	}
        	/* 复制新闻 */
        	
        	/* 
        	 * 引用新闻 
        	 * 轮播图引用图文  修改type   
        	 * 
        	 * */
        	if($type == 1 && I('figure') == 1){
        		$data['type'] = 2;
        	}
//         	
        	$data['create_time'] = date('Y-m-d H:i:s', time());
        	$data['refresh_time'] = time();
        	$news->add($data);
        }
//         if(!empty($news_recommend)){
			$_news_id = $data['news_id'] ? $data['news_id'] : $news_id;
            $recommendObj = M("vrnews1.news_recommend", NULL);
            $recommendObj->where(array('news_id' => $_news_id))->delete();
            /* for($i=0; $i<count($news_recommend); $i++ ){
                if(!empty($news_recommend[$i])){
                    $id   = substr($news_recommend[$i], 3);
                    $reference_id = substr($id,0, strpos($id, '&'));
                    $type = substr($news_recommend[$i], strrpos($news_recommend[$i], '=')+1);
                    $dataList[] = array('news_id'=>$data['news_id'] ? $data['news_id'] : $news_id, 'recommend_id'=>$reference_id, 'type' => $type);
                }
            } */
            
            $dataList = array();
            foreach($_POST['news_recommend'] as $list){
            	if($list && strpos($list, '=')){
            		parse_str($list, $_param);
            		$_param['news_id'] = $data['news_id']  ? $data['news_id'] : $news_id;
            		$_param['recommend_id'] = $_param['id'];
            		unset($_param['id']);
            		$dataList[] = $_param;
            	}
            }
            
            if(!empty($dataList)){
                $recommendObj->addAll($dataList);
            }
//         }

        $redis = new Redis();
        if($id_type == 'column_id'){
            $column_ids = $data['column_id'];
            $is_area = 0;
        }else if($id_type == 'area_id'){
            $column_ids  = I("area_id");
            $is_area = 1;
        }
        if($data['special_news_id'] != 0){
            $name = C('environment')."_special_list_" . $data['special_news_id'] . '_update';
            $column_list = C('environment').'_new_list_'.$is_area.'_'.$column_ids.'_update';
            $column_key = $redis->buildKey($column_list);
            $redis->set_redis($column_key, time());
        }else{
            $name = C('environment').'_new_list_'.$is_area.'_'.$column_ids.'_update';
        }
        $key = $redis->buildKey($name);
        $redis->set_redis($key, time());
        $info = $redis->get_redis($key);
        //如果是轮播图
        if($data['type'] == 2){
        	$this->success('发布成功！', U("Admin/NewsManage/figureList",array($id_type=>$id_val)));
        	exit();
        }
        if(I("is_special") != '2'){
            if($add_from == 1){
                $this->success('发布成功！', U('Admin/NewsManage/publish', array('news_id'=>$news_id, $id_type=>$id_val,'add_from'=>1)));
            }else{
                $this->success('发布成功！', U("Admin/News/index",array($id_type=>$id_val)));
            }
        }else{
        	$this->success('发布成功！', U("Admin/Special/index?",array($id_type=>$id_val,'news_id'=>$data['special_news_id'])));
        }
        
    }

    /**
     * 设置通栏弹窗
     */
    public function setFull(){
        $news_id = I("news_id");
        $column_id = I("column_id");
        $is_area = I("is_area");
        $special_news_id = I("special_news_id");
        $news      = M("vrnews1.news", NULL);
        $news_info = $news->field("full_title,full_subtitle,full_cover_image")->find($news_id);
        $this->assign("news_id", $news_id);
        $this->assign("news_info", $news_info);
        $this->assign("column_id", $column_id);
        $this->assign("is_area", $is_area);
        $this->assign("special_news_id", $special_news_id);
        $this->display('setfull');
    }

    /**
     * 设置通栏
     */
    public function addFull(){
        $title     = I("title");
        $subtitle  = I("subtitle");
        $cover_img = '';
        $news_id   = I("news_id");
        $column_id = I("column_id");
        $is_area = I("is_area");
        $status    = I("status");
        $special_news_id    = I("special_news_id");
        $news = M("vrnews1.news", NULL);
        $newsInfo = $news->find($news_id);
        $hidimg   = I("hidimg");

        if($status == '1'){  //设置通栏
            if($_FILES['imageFile']['name']) {
                $newsModel = new NewsModel();  //上传封面图
                $imageUrl = $newsModel->uploadImage();
                $data['full_cover_image'] = $imageUrl;
                $cover_img = $imageUrl;
//                print_r($imageUrl);die;
            }else if($hidimg){
                $data['full_cover_image'] = $hidimg;
                $cover_img = $hidimg;
            }else{
                $data['full_cover_image'] = '';
            }
            
            $data['full_status']   = $status;
            $data['full_title']    = $title;
            $data['full_subtitle'] = $subtitle;
            $data['update_time']   = time();
            if($newsInfo['special_entry'] == 1){
                // 若为 入口 将专题冗余字段更改
                $data1['special_title']    = $title;
                $data1['special_abstract'] = $subtitle;
                $data1['special_image']    = $cover_img;
                $data1['special_type']     = 8; //通栏
                $news->where("news_id = ".$newsInfo['special_news_id'])->data($data1)->save();
            }
        }else{  //清空通栏
            $data['full_status'] = $status;
            $data['full_title']  = '';
            $data['full_subtitle']    = '';
            $data['full_cover_image'] = '';
            $data['update_time']      = time();
            if($newsInfo['special_entry'] == 1){
                //若为 入口 将专题冗余字段更改 为 原内容
                $data2['special_title']    = $newsInfo['title'];
                $data2['special_abstract'] = $newsInfo['subtitle'];
                if($newsInfo['type'] == 5){
                    $data2['special_image']    = $newsInfo['content'];
                }else{
                    $data2['special_image']    = $newsInfo['cover_image'];
                }
                $data2['special_type']     = $newsInfo['type'];
                $news->where("news_id = ".$newsInfo['special_news_id'])->data($data2)->save();
            }
        }
        
        $result = $news->where(array('news_id' => $news_id))->save($data);
        $redis = new Redis();
        if(!empty($special_news_id)){
            $name = C('environment')."_special_list_" . $special_news_id . '_update';
            $column_list = C('environment').'_new_list_'.$is_area.'_'.$column_id.'_update';
            $column_key  = $redis->buildKey($column_list);
            $redis->set_redis($column_key, time());
        }else{
            $name = C('environment').'_new_list_'.$is_area.'_'.$column_id.'_update';
        }
        $key = $redis->buildKey($name);
        $redis->set_redis($key, time());
        echo $result;exit();
    }

    /**
     * 刷新新闻
     */
    public function refreshNews(){
        $news_id = I("news_id");
        $column_id = I("column_id");
        $is_area = I("is_area");
        $special_news_id = I("special_news_id");
        $news = M("vrnews1.news", NULL);
        $data['update_time'] = time();
        $data['refresh_time'] = time();
        $result = $news->where(array('news_id'=>$news_id))->save($data);
        if($result){
            $redis = new Redis();
            if(!empty($special_news_id)){
                $name = C('environment')."_special_list_" . $special_news_id . '_update';
            }else{
                $name = C('environment').'_new_list_'.$is_area.'_'.$column_id.'_update';
            }
            $key = $redis->buildKey($name);
            $redis->set_redis($key, time());
        }
        echo $result;exit();
    }

    /**
     * 修改权重
     */
    public function editWeight(){
        $news_id = I("news_id");
        $weight  = I("weight");
        $column_id = I("column_id");
        $is_area = I("is_area");
        $special_news_id  = I("special_news_id",0);
        $redis = new Redis();
        if(!empty($special_news_id)){
            $name = C('environment')."_special_list_" . $special_news_id . '_update';
        }else{
            $name = C('environment').'_new_list_'.$is_area.'_'.$column_id.'_update';
        }
        $key = $redis->buildKey($name);
        $redis->set_redis($key, time());
        $news    = M("vrnews1.news", NULL);
        $data['weight'] = $weight;
        $res = $news->where(array('news_id'=>$news_id))->save($data);
        echo $res;exit;
    }
    
    /**
     * 轮播图列表
     */
    public function figureList(){
    	$column_id = I("column_id") ? I("column_id") : '';
    	$area_id   = I("area_id") ? I("area_id") : '';
    	$id_type = '';
    	$id_val  = '';
    	if($column_id){
    		$id_type = "column_id";
    		$id_val  = $column_id;
    	}else{
    		$id_type = "area_id";
    		$id_val  = $area_id;
    	}
    	if(!$id_type || !$id_val){
    		$this->error("请先选择 栏目！");
    	}
    	//        $category = $this->getCategory();
    	//        $this->assign("category", $category);
    	$formParam['newsTitle'] = I('param.newsTitle') ? I('param.newsTitle') : "";
    	$and_newstitle = '';
    	
    	if(!empty(I('param.newsTitle'))){
    		$and_newstitle = " and title like '%".I('param.newsTitle')."%'";
    		$formParam['newsTitle'] = I('param.newsTitle');
    	}else{
    		$formParam['newsTitle'] = "";
    	}
    	
    	//搜索 栏目
    	$news_type = (int)I('news_type');
    	if($news_type && $news_type > 1){
    		$and_type = ' AND vrnews1.news.type='.$news_type.' ';
    		if($news_type == $this->video_type){
    			//普通视频
    			$and_type = ' AND vrnews1.news.type=4 AND vrnews1.news_video.category=1';
    		}elseif($news_type == $this->vr_video_type){
    			//VR视频
    			$and_type = ' AND vrnews1.news.type=4 AND vrnews1.news_video.category=2';
    		}elseif($news_type == 7){
    			$and_type = " AND vrnews1.news.type=$this->text ";
    		}elseif($news_type == 8){
    			$and_type = " AND vrnews1.news.type=$this->image_text ";
    		}
    		 
    	}
    	// 处理 搜索项内的 新闻分类
    	$and_newstype = '';
    	if($column_id) {
    		$cates = new NewsColumnTypeModel();
    		$get_cates = $cates->where("column_id = $column_id and weight >= 70 and status = 1")->field("type_id,name")->select();
    		if(!I('param.category')) { //没有筛选项
    			$formParam['categoryId'] = $get_cates;
    			$this->assign('cate_sel', "selected");
    		}else{ //选择筛选项
    			$and_newstype = " and type_id = ".I('param.category');
    			foreach($get_cates as $key=>$val){
    				$formParam['categoryId'][$key]['type_id'] = $val['type_id'];
    				$formParam['categoryId'][$key]['name']    = $val['name'];
    				if($val['type_id'] == I('param.category')){
    					$formParam['categoryId'][$key]['selected'] = "selected";
    				}else{
    					$formParam['categoryId'][$key]['selected'] = '';
    				}
    			}
    		}
    	}else{
    		$formParam['categoryId'] = '';
    	}
    	
    	
    	$this->assign("formParam", $formParam);
    	
    	$currentPage = I('param.p', 0);
    	if ($currentPage <= 0) {
    		$currentPage = 1;
    	}
    	
    	$pageSize = '10';
    	$pageStart = ($currentPage - 1) * $pageSize;
    	$pageEnd = $currentPage * $pageSize;
    	$news = M("vrnews1.news", NULL);
    	$carousel_figure_list = $news->where("$id_type = $id_val and special_news_id = 0  and vrnews1.news.status=0 and  vrnews1.news.type =2" .$and_newstitle .$and_newstype. $and_type)
    	->join("vradmin1.admin_user on admin_user.admin_id = vrnews1.news.creator_id", 'left')
    	->join("vrnews1.news_video on vrnews1.news_video.news_id = vrnews1.news.news_id", 'left')
    	->field("vrnews1.news.*, vradmin1.admin_user.username,year(vrnews1.news.create_time) as year1,month(vrnews1.news.create_time) as month1,day(vrnews1.news.create_time) as day1,year(from_unixtime(refresh_time)) as year,month(from_unixtime(refresh_time)) as month,day(from_unixtime(vrnews1.news.refresh_time)) as day,from_unixtime(refresh_time) as refresh_time")
    	->Order("case  when year is null then year1 else year end desc,
            	case  when month is null then month1 else month end desc,
            	case  when day is null then day1 else day end desc,
            	weight desc,
            	refresh_time desc,
            	create_time desc")
        ->limit($pageStart,$pageSize)
    	->select();
    	
    	$carousel_figure_count = $news->where("$id_type = $id_val and special_news_id = 0 and vrnews1.news.status=0 and  vrnews1.news.type =2" .$and_newstitle .$and_newstype. $and_type)
    	->join("vradmin1.admin_user on admin_user.admin_id = vrnews1.news.creator_id", 'left')
    	->join("vrnews1.news_video on vrnews1.news_video.news_id = vrnews1.news.news_id", 'left')
    	->field("vrnews1.news.*, vradmin1.admin_user.username,year(vrnews1.news.create_time) as year1,month(vrnews1.news.create_time) as month1,day(vrnews1.news.create_time) as day1,year(from_unixtime(refresh_time)) as year,month(from_unixtime(refresh_time)) as month,day(from_unixtime(vrnews1.news.refresh_time)) as day,from_unixtime(refresh_time) as refresh_time")
    	->count();
    	
    	if(!empty($carousel_figure_list)){
    		foreach($carousel_figure_list as $key=>&$val){
    			$str = '';
    			if($val['web_pub'] == 1){
    				$str .= '门户网站, ';
    			}
    			if($val['app_pub'] == 1){
    				$str .= 'APP';
    			}
    			$carousel_figure_list[$key]['pub'] = rtrim($str, ',');
    	
    			$val['type_zh'] = $this->_getNewType($val);
    			
    			/*打赏汇闻币*/
    			$carousel_figure_list[$key]['hw_money_total'] = '0';
    			$news_reward = new NewsRewardModel();
    			$news_reward_list = $news_reward->where("news_id = ".$val['news_id'])->field("hw_money")->select();
    			foreach($news_reward_list as $reward_key=>$reward_value){
    				$carousel_figure_list[$key]['hw_money_total'] +=$reward_value['hw_money'];//number_format($reward_value['hw_money'],'2');
    			}
    			
    		}
    		unset($val);
    	}
    	
    	$page = $this->page($carousel_figure_count ,10);
    	$this->assign("page", $page->show('Admin'));
    	$this->assign("id_type",$id_type);
    	$this->assign("id_val",$id_val);
    	$this->assign("carousel_figure_data", $carousel_figure_list);
    	$this->display();
    }

    /**
     * 轮播图
     */
    public function figure(){
        $column_id = I("column_id");
        $area_id = I("area_id");
        //返回 老版本 1，前端交互0 ，默认0
        $return_val = I("return_val") ? I("return_val") : 0;
        $id_type = '';
        $id_val  = '';
        if($column_id){
            $id_type = "column_id";
            $id_val  = $column_id;
        }else{
            $id_type = "area_id";
            $id_val  = $area_id;
        }
        $news_id   = I("news_id");
        if(!$id_type){
            $this->error("查找新闻失败！");
        }
        $column_type_list = array();
        if($column_id) {
            $column_type = M("vrnews1.column_type", 'news_');
            $column_type_list = $column_type->where(array("column_id" => $column_id, 'status' => 1))->field("type_id, name")->select();
            $this->assign("type_list", $column_type_list);
        }
        $news = M("vrnews1.news", NULL);
        $news_info = $news->find($news_id);
        /* 相关新闻 */
        $relation = new NewsRecommendModel();
        $news_info['relation'] = $relation->getNewsRelation($news_id);
        
        $this->assign("news_info", $news_info);
        $this->assign("id_type", $id_type);
        $this->assign("id_val", $id_val);
        $this->assign("return_val",$return_val);
        $this->display();
    }

    /**
     * 图集
     */
    public function pictureAdd(){
        $column_id = I("column_id") ? I("column_id") : '';
        $area_id   = I("area_id") ? I("area_id") : '';
        //返回 老版本 1，前端交互0 ，默认0
        $return_val = I("return_val") ? I("return_val") : 0;
        $id_type = '';
        $id_val  = '';
        if($column_id){
            $id_type = "column_id";
            $id_val  = $column_id;
        }else{
            $id_type = "area_id";
            $id_val  = $area_id;
        }
        
        $type = I("type",'1'); //默认为1,新闻列表，2时为专题下的新闻列表
        $type_id = I("type_id",''); //对应的专题分栏的id
        $special_id = I("special_id"); //专题id
        $news_id   = I("news_id");

        if(!$id_type){
            $this->error("查找新闻失败！");
        }

        if($type == '2' ){
            $special_column_type = M("vrnews1.column_type",'special_');
            $special_column_list = $special_column_type->where('status = 1 and news_id = '.$special_id)->field('type_id,name')->select();

            $this->assign("type", $type);
            $this->assign("type_id", $type_id);
            $this->assign("special_id", $special_id);
            $this->assign("special_column_type_list", $special_column_list);
        }else{
            $column_type_list = array();
            if($column_id) {
                $column_type = M("vrnews1.column_type", 'news_');
                $column_type_list = $column_type->where(array('column_id' => $column_id, 'status' => 1))->field("type_id, name")->select();
                $this->assign("type_list", $column_type_list);
            }
        }
        $news = M("vrnews1.news", NULL);
        $news_info = $news->join("vrnews1.news_video on news_video.news_id = news.news_id",'left')
            ->where(array('news.news_id'=>$news_id))->find();
        $relation = new NewsRecommendModel();
        $news_info['relation'] = $relation->getNewsRelation($news_id);
        $count = 1;
        if(!empty($news_info)){
            $content = json_decode($news_info['content']);
            $count = count($content)+1;
        }
        if($news_id) $news_info['news_id'] = $news_id;
        $this->assign("news_info", $news_info);
        $this->assign("type_id", $type_id);
        $this->assign("special_id", $special_id);
        $this->assign("news_id", $news_id);
        $this->assign("id_type", $id_type);
        $this->assign("id_val", $id_val);
        $this->assign("count", $count);
        $this->assign("return_val",$return_val);

        $this->display('pictureadd');
    }

    /**
     * 图集入库
     */
    public function addImage(){
//        var_dump($_REQUEST);
        $array = array_filter(I("post.update"),create_function('$v','return !empty($v);'));
        $update_arr = array_values($array);
        unset($_FILES['imageFiles']);
//        print_r($_FILES);die;
        $news_id = I("post.news_id","");
        $data['title']    = I("post.title");
        $data['subtitle'] = I("post.subTitle");
        $data['keywords'] = I("post.keyword");
        $source           = I("post.source");
        $data['weight']    = (int)(I("post.weight") ? I("post.weight") :'70');
        $data['tags']     = I("post.tags");
        $data['abstract'] = I("post.abstract") ? I("post.abstract") : '';
        $data['top_status']  = I("post.top_status");
        $data['special_news_id']  = I("post.special_id") ? I("post.special_id") : 0;
        $data['type']        = I("post.type");
        $data['type_id']     = !empty(I("post.column_type")) ? I("post.column_type") : (I("post.special_type") ? I("post.special_type") : 0);
        $pub                 = I("post.pub");
        $reference           = I("post.reference");
        $data['outer_url']   = I("post.outer_url");
        $outer_url_pub    = I("post.outer_url_pub");
        if($outer_url_pub == '1'){
        	if(empty($data['outer_url'])){
				return $this->error("无外链地址！");
			}
        	$data['outer_url_ishot'] = '1';
        }else{
        	$data['outer_url_ishot'] = '0';
        }
        $data['is_watermark']      = I("post.water");
//        $description         = I("post.pictureDescs");
        $image_title         = I("post.pictureTag");
//        $data['content']     = json_encode(I("post.newsContent"));

        if(!$news_id){
            $data['news_id'] = time().$this->getRange();
//            $news_id = $data['news_id'];
        }
        $data['source_name'] = $source;
        //查看 来源 如果存在获取来源ID 并保存，如果没有 则新添加一条来源并保存 来源ID
        $source_have = new NewsSourceModel();
        $have_id = $source_have->GetId($source);

        if($have_id){
            $data['source_id'] = $have_id;
        }else{
            $data['source_id'] = '';
        }

        $data['column_id'] = I("column_id")? I("column_id") : '';
        $data['area_id']   = I("area_id") ? I("area_id") : '';
        $id_type = '';
        $id_val  = '';
        if(I("column_id")){
            $id_type = "column_id";
            $id_val  = I("column_id");
        }else{
            $id_type = "area_id";
            $id_val  = I("area_id");
        }
        
        if(mb_strlen(trim($data['subtitle']),'UTF8') > 10){
        	return $this->error("副标题不能超过10个字");
        }
        
        if (!preg_match('/^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/', $data['weight'])) {
            return $this->error("权重必须为0到255的数字！");
        }
        
//        if(I("post.addOredit") == 0 && !empty($_FILES['imageFile']['name'])){

        if(!empty($_FILES['imageFile']['name']) || !empty($_FILES['imageFile']['name'][0])){
            $newsModel  = new NewsModel();  //上传封面图
            $imageUrl   = $newsModel->uploadImage();  //图片数组
//            print_r($imageUrl);
            $contentArr = array();
            if(!is_array($imageUrl) && $imageUrl != '没有文件被上传！'){  //一张
//                $item[] = $imageUrl;
//                if(!empty($description)){
//                    for($i=0;$i<count($description);$i++){
//                        if(!empty($description[$i])){
//                            $item['text'] = $description[$i];
//                        }
//                    }
//                }
                if(!empty($update_arr) && count($update_arr) > 0){
                    $contentArr[$update_arr[0]]['img'] = $imageUrl;
                    $contentArr[$update_arr[0]]['text'] = I('pictureDescs'.$update_arr[0]);
                }
//                var_dump($contentArr);exit();
            }else if(is_array($imageUrl)){   //多张
                for($i=0;$i<count($imageUrl);$i++){
//                    $item[] = $imageUrl[$i];
                    if(!empty($update_arr) && count($update_arr) > 0){
                        if($update_arr[$i]){
                            $contentArr[$update_arr[$i]]['img'] = $imageUrl[$i];
                            if(!empty(I('pictureDescs'.$update_arr[$i]))){
                                $contentArr[$update_arr[$i]]['text'] = I('pictureDescs'.$update_arr[$i]);
                            }
                        }
                    }else{
                        $contentArr[$i]['img'] = $imageUrl[$i];
                        if(!empty(I('pictureDescs'))){
                            $contentArr[$i]['text'] = I('pictureDescs'.$i);
                        }
                    }

                }
            }
        }
//        var_dump($contentArr);
        $contentArrs = array();
        if($_REQUEST['images']) {
            $images = $_REQUEST['images'];
            end($_REQUEST['images']);
            $key = key($_REQUEST['images']);
            for ($i = 1; $i <= $key; $i++) {
                if(!empty($images[$i])){
                    $items['img'] = $images[$i];
                    if (!empty($_REQUEST['pictureDescs'.$i])) {
                        $desc = $_REQUEST['pictureDescs'.$i];
                        $items['text'] = $desc;
                    }
                    $contentArrs[] = $items;
                }else{
                    if(!empty($contentArr[$i])){
                        $contentArrs[] = $contentArr[$i];
                        unset($contentArr[$i]);
                    }
                }
            }
        }
//        var_dump($contentArrs);exit();
        $content = array_merge($contentArrs, $contentArr);
        if(!empty($reference)){
            $id   = substr($reference, 3);
            $type = substr($reference, strrpos($reference, '=')+1);
            $data['reference_id']   = substr($id,0, strpos($id, '&'));
            $data['reference_type'] = $type;
        }
        if(empty($content) && $type != 1 && $type != 2){
            return $this->error("无图片不能保存！");
        }
//        var_dump($content);exit();
        $data['content'] = $content;
        //水印 处理
        $data['content'] = json_encode($this->setWatermark_img($news_id, $data['is_watermark'], $data['content']));

        if(!empty($pub)){
            if(in_array(1, $pub)){
                $data['app_pub'] = 1;
            }else{
                $data['app_pub'] = 0;
            }
            if(in_array(2, $pub)){
                $data['web_pub'] = 1;
            }else{
                $data['web_pub'] = 0;
            }
        }

        $news = M("vrnews1.news", NULL);
        
        if(IS_POST){
        	//解析内部url 如果有id 匹配原有数据
        	$diff = parse_str($_POST['reference']);
        	if($id){
        		$info = M("vrnews1.news", NULL)->where(array('news_id'=>$id))->field('type, content')->find();
        		if($info['type'] == 5){
        			//如果是图集  取出对应的内容
        			$data['content'] = $info['content'];
        			$data['type']    = $info['type'];
        		}else{
        			$data['type']    = $info['type'];
        		}
        	}
        }
        ob_start();
        $jssdk = new JssdkController("wx46fc101ae0317865", "ade9338f307169231203492f5e248ec6");
        if(!$news_id){
            $data['create_time'] = date('Y-m-d H:i:s', time());
        }else{
            $data['update_time'] = time();
        }
        $data['refresh_time'] = time();
        if($news_id){
            $share = C("admin_host").'/photos/' . $news_id . '.html';
            $file_name = SITE_PATH.'/data/html/photos/'.$news_id.'.html';
        }else{
            $share = C("admin_host").'/photos/' . $data['news_id'] . '.html';
            $file_name = SITE_PATH.'/data/html/photos/'.$data['news_id'].'.html';
        }
        if($type != 2){
            
            require_once (SITE_PATH.'/template/images_batch.html');
            $content = ob_get_contents();
            if(!$content){
                return $this->error("生产静态页面失败");
            }
            $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http';
            $jssdk = new JssdkController("wx46fc101ae0317865", "ade9338f307169231203492f5e248ec6");
            $signPackage = $jssdk->GetSignPackage();
            $appId = $signPackage["appId"];
            $timestamp = $signPackage["timestamp"];
            $nonceStr = $signPackage["nonceStr"];
            $signature = $signPackage["signature"];
            $description = $data['abstract'] ? $data['abstract'] : 'live.lawnewsw.com';
            $contents = json_decode($data['content'],true);
            if($data['new_title']){
                $title = $data['new_title'];
            }else{
                $title = $data['title'];
            }
            if($data['new_cover_image']){
                $cover_img =  $data['new_cover_image'];
            }else{
                if($data['cover_image']){
                    $cover_img = $data['cover_image'];
                }else{
                    $cover_img = C('h5_host').'/public/H5/images/default.png';
                }
            }
            if($data['new_cover_image']){
                $news_cover_img =  $data['new_cover_image'];
            }else{
                if($data['cover_image']){
                    $news_cover_img =  $data['cover_image'];
                }else{
                    $news_cover_img =  C('h5_host').'/public/H5/images/default.png';
                }
            }
            if($data['new_abstract']){
                $new_abstract = $data['new_abstract'];
            }else{
                if($data['abstract']){
                    $new_abstract = $data['abstract'];
                }else{
                    $new_abstract = 'live.lawnewsw.com';
                }
            }
            $text = '';
            foreach($contents as $key=>$val){
                $text .= '<img src="'.$val['img'].'" name="'.$key.'" onclick="clickImage()">';
            }
            $news_content = '';
            foreach($contents as $keys=>$value){
                $news_content .= '<div class="neirong" ';
                if($keys == 0) {$news_content .= 'style="display:block;" ';}
                $news_content .= '><div class="biaoti">
                        <div class="left">'.$data['title'].'</div>
                        <div class="right"></div>
                    </div>
                    <p>'.$value['text'].'</p>
                </div>';
            }
            $h5_host = C("h5_host");
            $host    = C("host");
            $content = str_replace('{{appId}}',$appId ,$content );
            $content = str_replace('{{timestamp}}',$timestamp ,$content );
            $content = str_replace('{{nonceStr}}',$nonceStr ,$content );
            $content = str_replace('{{signature}}',$signature ,$content );
            $content = str_replace('{{title}}',$title ,$content );
            $content = str_replace('{{share}}',$share ,$content );
            $content = str_replace('{{description}}',$description ,$content );
            $content = str_replace('{{cove_img}}',$cover_img ,$content );
            $content = str_replace('{{new_cover_img}}',$news_cover_img ,$content );
            $content = str_replace('{{text}}',$text ,$content );
            $content = str_replace('{{abstract}}',$new_abstract ,$content );
            $content = str_replace('{{content}}',$news_content ,$content );
            $content = str_replace('{{h5_host}}',$h5_host ,$content );
            $content = str_replace('{{host}}',$host ,$content );
            file_put_contents($file_name, $content);
            ob_end_clean();
        }
        if($news_id){
            $news->where('news_id = '.$news_id)->data($data)->save();
        }else{
        	/* 复制新闻 */
        	if($type == 2){
        		$_news_data = $news->where(array('news_id'=>$id))->find();
        		if(!$_news_data){$this->error('没有找到复制的新闻，请检查ID');exit();}
        		/* 轮播图只能复制图文的新闻 */
        		if(I('type') == 2 && $_news_data['type'] != 5){
        			$this->error('只能复制图集新闻');
        			exit();
        		}
        		
        		$data['title']    		  = $_news_data['title'];
        		$data['subtitle'] 		  = $_news_data['subtitle'];
        		$data['keywords']		  = $_news_data['keywords'];
        		$data['source_id']		  = $_news_data['source_id'];
        		$data['source_name'] 	  = $_news_data['source_name'];
        		$data['tags'] 			  = $_news_data['tags'];
        		$data['abstract'] 		  = $_news_data['abstract'];
        		$data['cover_image'] 	  = $_news_data['cover_image'];
        		$data['content'] 		  = $_news_data['content'];
        		$data['outer_url'] 		  = $_news_data['outer_url'];
        		$data['outer_url_ishot']  = $_news_data['outer_url_ishot'];
        		$data['app_pub'] 		  = $_news_data['app_pub'];
        		$data['web_pub'] 	 	  = $_news_data['web_pub'];
        		//$data['is_watermark'] = $_news_data['is_watermark'];
        		$data['weight'] 		  = $_news_data['weight'];
        		$data['top_status'] 	  = $_news_data['top_status'];
        		$data['full_status'] 	  = $_news_data['full_status'];
        		$data['full_title'] 	  = $_news_data['full_title'];
        		$data['full_subtitle']	  = $_news_data['full_subtitle'];
        		$data['full_cover_image'] = $_news_data['full_cover_image'];
        		$data['app_pub'] 		  = $_news_data['app_pub'];
        	
        		$data['reference_id']	  = 0;
        		$data['reference_type']   = 0;
                ob_start();
                
                require_once (SITE_PATH.'/template/images_batch.html');
                $content = ob_get_contents();
                if(!$content){
                    return $this->error("生产静态页面失败");
                }
                $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http';
                $jssdk = new JssdkController("wx46fc101ae0317865", "ade9338f307169231203492f5e248ec6");
                $signPackage = $jssdk->GetSignPackage();
                $appId = $signPackage["appId"];
                $timestamp = $signPackage["timestamp"];
                $nonceStr = $signPackage["nonceStr"];
                $signature = $signPackage["signature"];
                $description = $data['abstract'] ? $data['abstract'] : 'live.lawnewsw.com';
                $contents = json_decode($data['content'],true);
                if($data['new_title']){
                    $title = $data['new_title'];
                }else{
                    $title = $data['title'];
                }
                if($data['new_cover_image']){
                    $cover_img =  $data['new_cover_image'];
                }else{
                    if($data['cover_image']){
                        $cover_img = $data['cover_image'];
                    }else{
                        $cover_img = C('h5_host').'/public/H5/images/default.png';
                    }
                }
                if($data['new_cover_image']){
                    $news_cover_img =  $data['new_cover_image'];
                }else{
                    if($data['cover_image']){
                        $news_cover_img =  $data['cover_image'];
                    }else{
                        $news_cover_img =  C('h5_host').'/public/H5/images/default.png';
                    }
                }
                if($data['new_abstract']){
                    $new_abstract = $data['new_abstract'];
                }else{
                    if($data['abstract']){
                        $new_abstract = $data['abstract'];
                    }else{
                        $new_abstract = 'live.lawnewsw.com';
                    }
                }
                $text = '';
                foreach($contents as $key=>$val){
                    $text .= '<img src="'.$val['img'].'" name="'.$key.'" onclick="clickImage()">';
                }
                $news_content = '';
                foreach($contents as $keys=>$value){
                    $news_content .= '<div class="neirong" ';
                    if($keys == 0) {$news_content .= 'style="display:block;" ';}
                    $news_content .= '><div class="biaoti">
                        <div class="left">'.$data['title'].'</div>
                        <div class="right"></div>
                    </div>
                    <p>'.$value['text'].'</p>
                </div>';
                }
                $h5_host = C("h5_host");
                $host    = C("host");
                $content = str_replace('{{appId}}',$appId ,$content );
                $content = str_replace('{{timestamp}}',$timestamp ,$content );
                $content = str_replace('{{nonceStr}}',$nonceStr ,$content );
                $content = str_replace('{{signature}}',$signature ,$content );
                $content = str_replace('{{title}}',$title ,$content );
                $content = str_replace('{{share}}',$share ,$content );
                $content = str_replace('{{description}}',$description ,$content );
                $content = str_replace('{{cove_img}}',$cover_img ,$content );
                $content = str_replace('{{new_cover_img}}',$news_cover_img ,$content );
                $content = str_replace('{{text}}',$text ,$content );
                $content = str_replace('{{abstract}}',$new_abstract ,$content );
                $content = str_replace('{{content}}',$news_content ,$content );
                $content = str_replace('{{h5_host}}',$h5_host ,$content );
                $content = str_replace('{{host}}',$host ,$content );
                file_put_contents($file_name, $content);
                ob_end_clean();
        	}
        	/* 复制新闻 */
            
            $news->add($data);
        }
        $redis = new Redis();
        if($id_type == 'column_id'){
            $column_ids = $data['column_id'];
            $is_area = 0;
        }else if($id_type == 'area_id'){
            $column_ids  = I("area_id");
            $is_area = 1;
        }
        if($data['special_news_id'] != 0){
            $name = C('environment')."_special_list_" . $data['special_news_id'] . '_update';
            $column_list = C('environment').'_new_list_'.$is_area.'_'.$column_ids.'_update';
            $column_key = $redis->buildKey($column_list);
            $redis->set_redis($column_key, time());
        }else{
            $name = C('environment').'_new_list_'.$is_area.'_'.$column_ids.'_update';
        }
        $key = $redis->buildKey($name);
        $redis->set_redis($key, time());
        if($data['special_news_id'] !='0'){
//            $this->success('发布成功！', U("Admin/News/index",array($id_type=>$id_val)));
            $this->success('发布成功！', U("Admin/Special/index",array($id_type=>$id_val,'news_id'=>$data['special_news_id'])));
        }else{
            $this->success('发布成功！', U("Admin/News/index?".$id_type."=".$id_val));
        }
    }
    
    function setWatermark($news_id = null, $is_watermark = null, $content = null){
        // jpg/png 图片处理
        if($news_id){
            $news = M("vrnews1.news", NULL);
            $news_info = $news->find($news_id);
            if($news_info){
                if($is_watermark == '0'){ //不添加水印 将含有 /s 的去掉
                    $content = json_encode($content);
                    $content = preg_replace('/(src=\\\"http:\\\\\/\\\\\/vrlive-.*?(jpg|png))\\\\\/s/', '$1', $content);
                    return json_decode($content);
                }else{ //添加水印 将没有 /s 的添加
                    $content = json_encode($content);
                    \Think\Log::record($content,"INFO");
                    $content = preg_replace('/(src=\\\"http:\\\\\/\\\\\/vrlive-.*?(jpg|png))\\\\\/s/', '$1', $content);
                    $content = preg_replace('/(src=\\\"http:\\\\\/\\\\\/vrlive-.*?(jpg|png))/', '$1/s', $content);
//                    \Think\Log::record($content,"INFO");
                    return json_decode($content);
                }
            }
        }else{
            //添加水印  加 /s
            if($is_watermark == 1){
                $content = json_encode($content);
                $content = preg_replace('/(src=\\\"http:\\\\\/\\\\\/vrlive-.*?(jpg|png))/', '$1/s', $content);
//                \Think\Log::record($content,"INFO");
                return json_decode($content);
            }
        }
        return $content;
    }

    function setWatermark_img($news_id = null, $is_watermark = null, $content = null){
        // jpg/png 图片处理
        if($news_id){
            $news = M("vrnews1.news", NULL);
            $news_info = $news->find($news_id);
            if($news_info){
                if($is_watermark == '0'){ //不添加水印 将含有 /s 的去掉
                    $content = json_encode($content);
                    $content = preg_replace('/(http:\\\\\/\\\\\/vrlive-.*?(jpg|png))\\\\\/s/', '$1', $content);
                    return json_decode($content);
                }else{ //添加水印 将没有 /s 的添加
                    $content = json_encode($content);
                    \Think\Log::record($content,"INFO");
                    $content = preg_replace('/(http:\\\\\/\\\\\/vrlive-.*?(jpg|png))\\\\\/s/', '$1', $content);
                    $content = preg_replace('/(http:\\\\\/\\\\\/vrlive-.*?(jpg|png))/', '$1/s', $content);
//                    \Think\Log::record($content,"INFO");

                    return json_decode($content);
                }
            }
        }else{
            //添加水印  加 /s
            if($is_watermark == 1){
                $content = json_encode($content);
                $content = preg_replace('/(http:\\\\\/\\\\\/vrlive-.*?(jpg|png))/', '$1/s', $content);
//                \Think\Log::record($content,"INFO");

                return json_decode($content);
            }
        }
        return $content;
    }
    
}